<?php
require_once '../api.php';
require_once '../DB.php';
/**
* 处理接口公共业务
*/
class Common
{
	public $params;
	public $app;
	/**
	 * 检查版本信息
	 * @return string [description]
	 */
	public function check()
	{
		$this->params['app_id'] = isset($_POST['app_id']) ? $_POST['app_id'] : '';
		$this->params['version_id'] = isset($_POST['version_id']) ? $_POST['version_id'] : '';
		$this->params['version_mini'] = isset($_POST['version_mini']) ? $_POST['version_mini'] : '';
		$this->params['did'] = isset($_POST['did']) ? $_POST['did'] : '';
		$this->params['encrypt_did'] = isset($_POST['encrypt_did']) ? $_POST['encrypt_did'] : '';
		if (!is_numeric($this->params['app_id']) || !is_numeric($this->params['version_id'])) {
			return 1;
		}
		//判读app是否需要加密
		$this->app = $this->getVersion($this->params['app_id'], 'app');
		if (!$this->app) {
			return 2;
		}
		if ($this->app['is_encryption'] && $this->params['encrypt_did'] != md5($this->params['did'] . $this->app['key'])) {
			return 3;
		}
	}
	/**
	 * 获取版本信息
	 * @param  int $id    [description]
	 * @param  string $table [description]
	 * @return array        [description]
	 */
	public function getVersion($id, $table)
	{
		$sql = 'SELECT * FROM ' . $table . ' WHERE app_id=' . $id . ' AND status=1 LIMIT 1';
		$connect = DB::getInstance()->connect();
		$result = $connect->query($sql);
		return $result->fetch_assoc();
	}
}